layui.define(['table','form','element'], function(exports){
    var $ = layui.$,form = layui.form,table = layui.table,element = layui.element;

    element.on('tab(tab-info)', function(data){
        if(data.index == 0){
            table.reload('LAY-operate-list');
        }else if(data.index == 1){
            table.reload('LAY-withdraw-payee-list');
        }else if(data.index == 2){
            table.reload('LAY-deposit-list');
        }else if(data.index == 3){
            table.reload('LAY-income-list');
        }else if(data.index == 4){
            table.reload('LAY-pay-list');
        }else if(data.index == 5){
            table.reload('LAY-withdraw-list');
        }
    })

    table.render({
        elem: '#LAY-operate-list'
        ,url: layui.setter.webRoot + '/market/payAccount/operate/list.data'
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', title: 'ID'}
            ,{field:'moneyTypeName', title: '钱类型'}
            ,{field:'typeName', title: '操作方式'}
            ,{field:'moneyStr', title: '操作金额'}
            ,{field:'operatorTypeName', title: '操作人',templet:function(record){
                    return '【'+ record.operatorTypeName +'】' + record.operatorName;
                }}
            ,{field:'operateNote', title: '操作说明'}
            ,{field:'createDate', title: '操作时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-deposit-list'
        ,url: layui.setter.webRoot + '/market/payAccount/deposit/order/list.data'
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'payTypeName', title: '充值方式'}
            ,{field:'payMoneyStr', title: '充值金额'}
            ,{field:'receiptMoneyStr', title: '实际到账'}
            ,{field:'payStatusName', title: '支付状态'}
            ,{field:'payDate', title: '支付时间'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
            , {title: '操作', width: 100, align: 'center', templet: function (record) {
                    var html = "";
                    if(record.state == 1){
                        html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="rechargeOrder">充值</a>';
                        html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelRechargeOrder">取消</a>';
                    }
                    return html;
                }}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    //收入订单
    table.render({
        elem: '#LAY-income-list'
        ,url: layui.setter.webRoot + '/market/payAccount/order/list.data'
        ,where:{type:1}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'businessTradeNo', title: '业务订单号'}
            ,{field:'payMoneyStr', title: '支付金额'}
            ,{field:'payerAccountId', title: '支付账号'}
            ,{field:'payeeAccountId', title: '收款账号'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
            , {title: '操作', width: 100, align: 'center', templet: function (record) {
                    var html = "";
                    if(record.state == 3){
                        html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="settlementOrder"><i class="layui-icon layui-icon-edit"></i>结算</a>';
                    }
                    return html;
                }}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-pay-list'
        ,url: layui.setter.webRoot + '/market/payAccount/order/list.data'
        ,where:{type:2}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'businessTradeNo', title: '业务订单号'}
            ,{field:'payMoneyStr', title: '支付金额'}
            ,{field:'payerAccountId', title: '支付账号'}
            ,{field:'payeeAccountId', title: '收款账号'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-withdraw-list'
        ,url: layui.setter.webRoot + '/market/payAccount/withdraw/order/list.data'
        ,where:{type:1}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'moneyStr', title: '取款金额'}
            ,{field:'receivingTypeName', title: '收款人类型'}
            ,{field:'receivingId', title: '收款人ID'}
            ,{field:'receivingFaceImg', title: '收款人头像',templet:function(record){
                    if(record.receivingFaceImg) {
                        return '<image src="' + record.receivingFaceImg + '"/>';
                    }else{
                        return '无';
                    }
                }}
            ,{field:'receivingNickname', title: '收款人名称'}
            ,{field:'receivingPhone', title: '收款人电话'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-withdraw-payee-list'
        ,url: layui.setter.webRoot + '/market/payAccount/withdraw/payee/list.data'
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field:'receivingId', title: '收款人ID'}
            ,{field:'receivingNickname', title: '收款人昵称'}
            ,{field:'receivingPhone', title: '收款人电话'}
            ,{field:'receivingTypeName', title: '收款人类型'}
            ,{field:'receivingAppId', title: '收款应用ID'}
            ,{field:'createDate', title: '创建时间'}
            , {title: '操作', width: 100, align: 'center', templet: function (record) {
                    var html = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delWithdrawPayee"><i class="layui-icon layui-icon-edit"></i>删除</a>';
                    return html;
                }}
        ]]
        ,page: false
        ,height: 'full-220'
    });

    //监听搜索
    form.on('submit(LAY-operate-search)', function(data){
        var field = data.field;
        //执行重载
        table.reload('LAY-operate-list', {
            where: field
            ,page: {curr:1}
        });
    });

    //事件
    var active = {
        recharge:function () {
            layer.open({
                type: 2
                , title: '充值'
                , content: layui.setter.webRoot + '/market/payAccount/recharge.html'
                , maxmin: false
                , area: ['500px', '500px']
                , btn: ['取消']
            });
        },
        rechargeOrder:function (obj) {
            layer.open({
                type: 2
                , title: '充值'
                , content: layui.setter.webRoot + '/market/payAccount/recharge.html?tradeNo=' + obj.data.tradeNo + "&money=" + obj.data.payMoneyStr
                , maxmin: false
                , area: ['500px', '500px']
                , btn: ['取消']
            });
        }
        ,cancelRechargeOrder:function (obj){
            layer.confirm('确定要取消该充值吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/payAccount/recharge/cancel.do',
                    data:{tradeNo:obj.data.tradeNo},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,withdraw:function () {
            layer.open({
                type: 2
                , title: '取款'
                , content: layui.setter.webRoot + '/market/payAccount/withdraw.html'
                , maxmin: false
                , area: ['80%', '80%']
                , btn: ['取消']
            });
        }
        ,addWithdrawPayee: function(obj){
            layer.open({
                type: 2
                ,title: '新增取款用户'
                ,content: layui.setter.webRoot + '/market/payAccount/withdraw/payee/add.html'
                ,maxmin: false
                ,area: ['450px', '450px']
                ,btn: [ '取消']
                ,end:function (){
                    table.reload('LAY-withdraw-payee-list');
                }
            });
        }
        ,delWithdrawPayee:function(obj){
            layer.confirm('确定要删除该取款用户吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/payAccount/withdrawPayee/del.do',
                    data:{id:obj.data.id},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,settlementOrder:function(obj){
            layer.confirm('确定要结算该订单吗？请确保业务订单已完成', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/payAccount/order/settlement.do',
                    data:{businessTradeNo:obj.data.businessTradeNo},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            table.reload('LAY-income-list');
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-deposit-list)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    table.on('tool(LAY-income-list)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    table.on('tool(LAY-withdraw-payee-list)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    exports('cloud_pay_account_index', {})
});